﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BYSJ.Model;
using BYSJ.Web.Filters;
using BYSJ.IBLL;
using BYSJ.BLL;
using BYSJ.Web.Models;
using BYSJ.Web.Models.shipin;
using BYSJ.Web.Commmend;
namespace BYSJ.Web.Controllers
{
    public class HomeController : Controller
    {
        //服务层接口
        IUserSetService UserServic = null;
        //用户对象
        //UserSet UserDbMoles = new UserSet();
        //video表查询
        IVideoSetService VidelServer = null;
        //视频子页查询
        IVideoShowSetService videoShow = null;
        //videl表查询结果
        IvideojiluSetService videojilu = null;

        IFileMangSetService fileServer = null;
        IAnswerSetService anServer = null;
        IvideojiluSetService jilu = null;
        IKaiYuanShiPinService kaiYuan = null;
        Commend commend = null;
        public HomeController()
        {
            jilu = new videojiluSetService();
            commend = new Commend();
            UserServic = new UserSetService();
            VidelServer = new VideoSetService();
            videoShow = new VideoShowSetService();
            videojilu = new videojiluSetService();
            fileServer = new FileMangSetService();
            anServer = new AnswerSetService();
            kaiYuan = new KaiYuanShiPinService();
        }
        // GET: /Home/
        //当文件分类的id=1时 为通知
        //当文件分类的id=2时 为课件
        //当文件分类的id=3时 为实验指导
        //当文件分类的id=4时 为替他
        //当文件分类的id=5时 为共享资源
        //当文件分类的id=6时 为老师推荐
        [AdminFilter]
        [ShenFen]
        public ActionResult Index(string Name)
        {
            //第一个侧边栏数据
           var shipinCebian= videojilu.LoadEntities(u => u.id!=null).OrderByDescending(a=>a.crettime).ToList();
           int totalCountTh = 0;
            //第二个侧边栏数据
           var dbfilesTh = fileServer.LoadPageEntities(8, 1, out totalCountTh,u=>u.FileIsdel == false&&u.Fileleibie>=5, false, a => a.FilecreatTime).ToList();
           //视图对象
            //小编推荐数据 查询
           int totalCountThs = 0;
           var videoTuiJian = VidelServer.LoadPageEntities(4, 1, out totalCountThs, u =>  u.imageurl !=null, false, a => a.id).ToList();
            //资料库一行数据 查出来所有的分享数据
           var dbfilesThziliao = fileServer.LoadEntities(u => u.Fileleibie>=5).OrderByDescending(a => a.id).ToList();
                 int totalCountThsa = 0;
           var dbanswer = anServer.LoadPageEntities(6, 1, out totalCountThsa, u =>  u.isdel ==false, false, a => a.creattime).ToList();
           //首页学习明星数据
           int totalCount;
           var jiluresulr = jilu.LoadPageEntities(9, 1, out totalCount, u => u.id != null, false, a => a.crettime).ToList();
          //开源视频数据
               int KytotalCount;
           var KaiYuanVideo = kaiYuan.LoadPageEntities(4, 1, out KytotalCount, u => u.isDel==false, false, a => a.CreatTime).ToList();
           UserView UserViesModel = commend.indexCebianlan(shipinCebian, dbfilesTh, videoTuiJian, dbfilesThziliao, dbanswer, jiluresulr, KaiYuanVideo);
           return View(UserViesModel);
        }
        [AdminFilter]
        [ShenFen]
        [start]
        public ActionResult ShiPin()
        {
            shiPin shipin = new shiPin();
            var VideoResult = VidelServer.LoadEntities(u=>u.name!=null).ToList();
            shipin = commend.ShiPinConvert(VideoResult);
            return View(shipin);
        }
        [AdminFilter]
        [ShenFen]
        public ActionResult ShiPinShow(int Id)
        {
            int id= Convert.ToInt32(Request.Cookies["cookiename"].Value);
            shipinshow shipin = new shipinshow();
            //查出所有视频信息  VideoShow中的videoid等于Id的数据
            var shipinShowresule = videoShow.LoadEntities(u => u.videoid == Id).ToList();
            //查出本视频和本用户关联的视频信息 videojilu表中的信息
            var shipinJiLuresule = videojilu.LoadEntities(u => u.VideoShowSet.videoid == Id && u.UserSet.id == id).ToList();
            shipin = commend.ShiPinShowConvert(shipinShowresule,shipinJiLuresule,id);
            //查询这个视频  然后根据userid分组
            shipin.yixuerenshu = videojilu.LoadEntities(u => u.VideoShowSet.videoid == Id).GroupBy(u => u.userid).ToList().Count().ToString();
          
            return View(shipin);
        }
    }
}
